<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  Request
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: mpRequest.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * HTTP Request class (very minimalistic version, stripped down to basics)
 *
 * @package     mpContenidoX
 * @subpackage  Request
 * @author      Murat Purc <murat@purc.de>
 */
class mpRequest
{

    /**
     * Self instance
     * @var  mpRequest
     */
    private static $_instance = null;

    /**
     * Assoziative user parameter
     * @var  array
     */
    private $_userParams = array();


    /**
     * Constructor
     *
     * @return  void
     */
    protected function __construct()
    {
    }


    /**
     * Prevent cloning
     *
     * @return  void
     */
    private function __clone()
    {
        // donut
    }


    /**
     * Returns self instance.
     *
     * @return  mpRequest
     */
    public static function getInstance()
    {
        if (self::$_instance == null) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }


    /**
     * Returns all parameter (user parameter + GET + POST + COOKIE)
     *
     * @return  array  Assoziative array
     */
    public function getParams()
    {
        $params = $this->_userParams;
        if (isset($_GET) && is_array($_GET)) {
            $params = array_merge($params, $_GET);
        }
        if (isset($_POST) && is_array($_POST)) {
            $params = array_merge($params, $_POST);
        }
        if (isset($_COOKIE) && is_array($_COOKIE)) {
            $params = array_merge($params, $_COOKIE);
        }
        return $params;
    }


    /**
     * Returns all GET parameter
     *
     * @return  array  Assoziative array
     */
    public function getGetParams()
    {
        if (isset($_GET) && is_array($_GET)) {
            return $_GET;
        }
        return array();
    }


    /**
     * Returns GET parameter by it's key
     *
     * @param   string  $key
     * @param   mixed   $default
     * @return  mixed
     */
    public function getGetParam($key, $default=null)
    {
        if (isset($_GET) && is_array($_GET) && isset($_GET[$key])) {
            return $_GET[$key];
        }
        return $default;
    }


    /**
     * Returns all POST parameter
     *
     * @return  array  Assoziative array
     */
    public function getPostParams()
    {
        if (isset($_POST) && is_array($_POST)) {
            return $_POST;
        }
        return array();
    }


    /**
     * Returns POST parameter by it's key
     *
     * @param   string  $key
     * @param   mixed   $default
     * @return  mixed
     */
    public function getPostParam($key, $default=null)
    {
        if (isset($_POST) && is_array($_POST) && isset($_POST[$key])) {
            return $_POST[$key];
        }
        return $default;
    }


    /**
     * Returns the parameter
     *
     * @param   string  $key
     * @param   mixed   $default
     * @return  mixed
     */
    public function getParam($key, $default=null)
    {
        $value = $this->_getParam($key);
        return ($value !== null) ? $value : $default;
    }


    /**
     * Sets the parameter
     *
     * @param   string  $key
     * @param   mixed   $value
     * @return  void
     */
    public function setParam($key, $value)
    {
        $this->_userParams[$key] = $value;
    }


    /**
     * Unsets the parameter
     *
     * @param   string  $key
     * @return  void
     */
    public function unsetParam($key)
    {
        if (isset($this->_userParams[$key])) {
            unset($this->_userParams[$key]);
        }
    }


    /**
     * Shortcut for mpRequest::getParam()
     *
     * @param   string  $key
     * @param   mixed   $default
     * @return  mixed
     */
    public function get($key, $default=null)
    {
        return $this->getParam($key, $default);
    }


    /**
     * Shortcut for mpRequest::setParam()
     *
     * @param   string  $key
     * @param   mixed   $value
     * @return  void
     */
    public function set($key, $value)
    {
        return $this->setParam($key, $value);
    }


    /**
     * Magic parameter getter
     *
     * @param   string  $key
     * @return  mixed|null
     */
    public function __get($key)
    {
        return $this->getParam($key);
    }


    /**
     * Magic parameter setter
     *
     * @param   string  $key
     * @param   mixed   $value
     * @return  void
     */
    public function __set($key, $value)
    {
        return $this->setParam($key, $value);
    }


    /**
     * Magic parameter isset check
     *
     * @param   string  $key
     * @return  void
     */
    public function __isset($key)
    {
        return ($this->_getParam($key) !== null);
    }


    /**
     * Magic parameter unsetter
     *
     * @param   string  $key
     * @return  void
     */
    public function __unset($key)
    {
        return $this->unsetParam($key);
    }


    /**
     * Checks if the request method is "GET".
     *
     * @return  bool
     */
    public function isGet()
    {
        return (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'GET');
    }


    /**
     * Checks if the request method is "POST".
     *
     * @return  bool
     */
    public function isPost()
    {
        return (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST');
    }


    /**
     * Internal function to get parameter
     *
     * @param   string  $key
     * @return  mixed
     */
    protected function _getParam($key, $default=null)
    {
        if (isset($this->_userParams[$key])) {
            return $this->_userParams[$key];
        } elseif (isset($_GET) && isset($_GET[$key])) {
            return $_GET[$key];
        } elseif (isset($_POST) && isset($_POST[$key])) {
            return $_POST[$key];
        } elseif (isset($_COOKIE) && isset($_COOKIE[$key])) {
            return $_COOKIE[$key];
        } else {
            return null;
        }
    }

}
